<?php

namespace App\Models\Oauth;

use Illuminate\Support\Facades\DB;
use App\Models\Model;
use App\Core\Utils\TwHelper\Random;

/**
 * 授权model
 */
class AuthorizeModel extends Model
{

    const TABLENAME = 'tw_open_authorize';
    //授权码 code的有效期，有效期 10分钟，
    //通常设为10分钟，客户端只能使用该码一次，否则会被授权服务器拒绝。
    const CODE_EXPIRES_IN = 600;

    //access_token令牌的有效期：60*60*2，2个小时
    const EXPIRES_IN = 7200;

    const STATUS_BAN = 1;//失效
    const STATUS_NOL = 0;//有效
    const TOKEN_TYPE = 'Bearer';

    /**
     * 初始化并返回数据库请求构建器
     *
     * @return Illuminate\Support\Facades\DB::table(TABLENAME)
     */
    public static function dbTable()
    {
        return DB::table(self::TABLENAME);
    }

    /**
     * 创建access_token数据
     * scope = basic
     */
    public static function createAuthorize($scope, $uid)
    {
        $data['user_id'] = $uid;
        //表示令牌类型，该值大小写不敏感，必选项，可以是bearer类型或mac类型。
        $data['token_type'] = self::TOKEN_TYPE;
        //表示访问令牌，必选项
        $data['access_token'] = Random::getUniqid();
        //表示过期时间，单位为秒。如果省略该参数，必须其他方式设置过期时间。
        $data['expires_in'] = self::EXPIRES_IN;
        //这个值一并返回给客户端，表示更新令牌，用来获取下一次的访问令牌
        $data['refresh_token'] = Random::getUniqid();
        //表示权限范围，如果与客户端申请的范围一致，此项可省略
        $data['scope'] = $scope;
        $data['created'] = $data['updated'] = time();
        //$data['status'] = self::STATUS_NOL;
        if (self::dbTable()->insertGetId($data)) {
            return array(
                'access_token' => $data['access_token'],
                'token_type' => $data['token_type'],
                'expires_in' => $data['expires_in'],
                'scope' => $data['scope'],
                'refresh_token' => $data['refresh_token']
            );
        } else {
            return false;
        }
    }

    public static function getAuthorizeById($id)
    {
        $where[] = ['id', '=', $id];
        return self::dbTable()
            ->where($where)
            ->first();
    }

    public static function getAuthorizeByAccessToken($accessToken)
    {
        $where[] = ['access_token', '=', $accessToken];
        return self::dbTable()
            ->where($where)
            ->first();
    }

    public static function getAuthorizeByRefreshToken($refreshToken)
    {
        $where[] = ['refresh_token', '=', $refreshToken];
        return self::dbTable()
            ->where($where)
            ->first();
    }

    /**
     * 刷新access_token
     */
    public static function refreshTokenByToken($accessToken)
    {
        $where ['access_token'] = $accessToken;
        $updateDate['updated'] = time();
        return self::dbTable()->where($where)->update($updateDate);
    }

    /**
     * 刷新access_token
     */
    public static function refreshTokenByRefreshToken($refresh_token)
    {
        $where ['refresh_token'] = $refresh_token;
        $updateDate['updated'] = time();
        return self::dbTable()->where($where)->update($updateDate);
    }

    /**
     * access_token 失效
     */
    public static function unsetToken($accessToken)
    {
        $where ['access_token'] = $accessToken;
        $updateDate['status'] = self::STATUS_BAN;//失效
        return self::dbTable()->where($where)->update($updateDate);
    }

}

?>